'use strict';

(function () {

    var employees = [
            {"id": 1, "firstName": "James", "lastName": "King", "managerId": 0, "managerName": "", "reports": 4, "title": "President and CEO", "department": "Corporate", "cellPhone": "617-000-0001", "officePhone": "781-000-0001", "email": "jking@fakemail.com", "city": "Boston, MA", "pic": "james_king.jpg", "twitterId": "@fakejking", "blog": "http://coenraets.org"},
            {"id": 2, "firstName": "Julie", "lastName": "Taylor", "managerId": 1, "managerName": "James King", "reports": 2, "title": "VP of Marketing", "department": "Marketing", "cellPhone": "617-000-0002", "officePhone": "781-000-0002", "email": "jtaylor@fakemail.com", "city": "Boston, MA", "pic": "julie_taylor.jpg", "twitterId": "@fakejtaylor", "blog": "http://coenraets.org"},
            {"id": 3, "firstName": "Eugene", "lastName": "Lee", "managerId": 1, "managerName": "James King", "reports": 0, "title": "CFO", "department": "Accounting", "cellPhone": "617-000-0003", "officePhone": "781-000-0003", "email": "elee@fakemail.com", "city": "Boston, MA", "pic": "eugene_lee.jpg", "twitterId": "@fakeelee", "blog": "http://coenraets.org"},
            {"id": 4, "firstName": "John", "lastName": "Williams", "managerId": 1, "managerName": "James King", "reports": 3, "title": "VP of Engineering", "department": "Engineering", "cellPhone": "617-000-0004", "officePhone": "781-000-0004", "email": "jwilliams@fakemail.com", "city": "Boston, MA", "pic": "john_williams.jpg", "twitterId": "@fakejwilliams", "blog": "http://coenraets.org"},
            {"id": 5, "firstName": "Ray", "lastName": "Moore", "managerId": 1, "managerName": "James King", "reports": 2, "title": "VP of Sales", "department": "Sales", "cellPhone": "617-000-0005", "officePhone": "781-000-0005", "email": "rmoore@fakemail.com", "city": "Boston, MA", "pic": "ray_moore.jpg", "twitterId": "@fakermoore", "blog": "http://coenraets.org"},
            {"id": 6, "firstName": "Paul", "lastName": "Jones", "managerId": 4, "managerName": "John Williams", "reports": 0, "title": "QA Manager", "department": "Engineering", "cellPhone": "617-000-0006", "officePhone": "781-000-0006", "email": "pjones@fakemail.com", "city": "Boston, MA", "pic": "paul_jones.jpg", "twitterId": "@fakepjones", "blog": "http://coenraets.org"},
            {"id": 7, "firstName": "Paula", "lastName": "Gates", "managerId": 4, "managerName": "John Williams", "reports": 0, "title": "Software Architect", "department": "Engineering", "cellPhone": "617-000-0007", "officePhone": "781-000-0007", "email": "pgates@fakemail.com", "city": "Boston, MA", "pic": "paula_gates.jpg", "twitterId": "@fakepgates", "blog": "http://coenraets.org"},
            {"id": 8, "firstName": "Lisa", "lastName": "Wong", "managerId": 2, "managerName": "Julie Taylor", "reports": 0, "title": "Marketing Manager", "department": "Marketing", "cellPhone": "617-000-0008", "officePhone": "781-000-0008", "email": "lwong@fakemail.com", "city": "Boston, MA", "pic": "lisa_wong.jpg", "twitterId": "@fakelwong", "blog": "http://coenraets.org"},
            {"id": 9, "firstName": "Gary", "lastName": "Donovan", "managerId": 2, "managerName": "Julie Taylor", "reports": 0, "title": "Marketing Manager", "department": "Marketing", "cellPhone": "617-000-0009", "officePhone": "781-000-0009", "email": "gdonovan@fakemail.com", "city": "Boston, MA", "pic": "gary_donovan.jpg", "twitterId": "@fakegdonovan", "blog": "http://coenraets.org"},
            {"id": 10, "firstName": "Kathleen", "lastName": "Byrne", "managerId": 5, "managerName": "Ray Moore", "reports": 0, "title": "Sales Representative", "department": "Sales", "cellPhone": "617-000-0010", "officePhone": "781-000-0010", "email": "kbyrne@fakemail.com", "city": "Boston, MA", "pic": "kathleen_byrne.jpg", "twitterId": "@fakekbyrne", "blog": "http://coenraets.org"},
            {"id": 11, "firstName": "Amy", "lastName": "Jones", "managerId": 5, "managerName": "Ray Moore", "reports": 0, "title": "Sales Representative", "department": "Sales", "cellPhone": "617-000-0011", "officePhone": "781-000-0011", "email": "ajones@fakemail.com", "city": "Boston, MA", "pic": "amy_jones.jpg", "twitterId": "@fakeajones", "blog": "http://coenraets.org"},
            {"id": 12, "firstName": "Steven", "lastName": "Wells", "managerId": 4, "managerName": "John Williams", "reports": 0, "title": "Software Architect", "department": "Engineering", "cellPhone": "617-000-0012", "officePhone": "781-000-0012", "email": "swells@fakemail.com", "city": "Boston, MA", "pic": "steven_wells.jpg", "twitterId": "@fakeswells", "blog": "http://coenraets.org"}
        ],

        findById = function (id) {
            var employee = null,
                l = employees.length,
                i;
            for (i = 0; i < l; i = i + 1) {
                if (employees[i].id === id) {
                    employee = employees[i];
                    break;
                }
            }
            return employee;
        },

        findByManager = function (managerId) {
            var results = employees.filter(function (element) {
                return managerId === element.managerId;
            });
            return results;
        },


    /* Report Execution */
        executeReport = function (queryName, org, year, quarter) {

            var report = [
                    {kpiname: "n:Total number of scaled category over a period of time",
                        value: "78", groupID: "1"},
                    {kpiName: "nNI: Number of events rated Need Improvement (NI)",
                        value: "35", groupID: "1"},
                    {kpiName: "nS:   Number of events rated Satisfactory (S)",
                        value: "42", groupID: "1"},
                    {kpiName: "nO:   Number of events rated Outstanding (O)",
                        value: "36", groupID: "1"},
                    {kpiName: "# of training hours conducted",
                        value: "36", groupID: "2"},
                    {kpiName: "# of training hours with full utilization",
                        value: "36", groupID: "2"}

                ],

                groupsList = [
                    {id: '1', text: "Response Time"},
                    {id: '2', text: "Training Schedule Attainment"}
                ];

            var result = {'groupList': groupsList, 'reportData': report};
            return result;
        },

        varList = function (queryName) {
            var result = {
                "asx:abap": {
                    "asx:values": {
                        "VARIABLES": {
                            "item": {
                                "VARI_NAME": "X_WHNUMB",
                                "VARI_PAR_SEL": "S",
                                "VARI_TEXT": "Warehouse Number",
                                "ENTRY_TYPE": "0",
                                "DATA_TYPE": "CHAR",
                                "OUTPUT_LEN": "000003",
                                "ABAP_TYPE": "C",
                                "OBJ_NAME": "0WHSE_NUM",
                                "INT_LEN": "000003",
                                "P_DECIMALS": "000000",
                                "P_SIGN": "",
                                "CASE_SENSE": "",
                                "CHECK_BOX": "",
                                "LOWVAL": "",
                                "HIGHVAL": "",
                                "SIGN": "I",
                                "OPT": "EQ",
                                "DEF_VAL": "",
                                "VARTYP": "1"
                            }
                        }
                    }
                }
            }
            return result;
        },

        f4Help = function (iobjnm, vname, queryName) {
            var result = {
                "asx:abap": {
                    "asx:values": {
                        "F4VALUE": {
                            "item": [
                                {
                                    "CHAVL_INT": "SH_9999",
                                    "CHAVL_INT_NC": "SH_9999",
                                    "CHAVL_EXT": "SH_9999",
                                    "CHAVL_EXT_NC": "SH_9999",
                                    "CHATEXT": {
                                        "TXTSH": "SH_9999",
                                        "TXTMD": "SH_9999",
                                        "TXTLG": "SH_9999"
                                    }
                                },
                                {
                                    "CHAVL_INT": "SH_9999.0704",
                                    "CHAVL_INT_NC": "SH_9999.0704",
                                    "CHAVL_EXT": "SH_9999.0704",
                                    "CHAVL_EXT_NC": "SH_9999.0704",
                                    "CHATEXT": {
                                        "TXTSH": "SH_9999.0704",
                                        "TXTMD": "SH_9999.0704",
                                        "TXTLG": "SH_9999.0704"
                                    }
                                }
                            ]
                        }
                    }
                }
            }

            return result;
        },

        varInfo = function (varTechName){
            var result = {
                "asx:abap": {
                    "asx:values": {
                        "VARIABLES": {
                            "VARI_NAME": "X_WHNUMB",
                            "VARI_TEXT": "",
                            "VARI_TEXT_L": "Warehouse Number",
                            "VARI_PAR_SEL": "S",
                            "ENTRY_TYPE": "1",
                            "OBJ_NAME": "0WHSE_NUM"
                        }
                    }
                }
            }
            return result;
        },

        getAppSetting = function () {
            var yearList = ['2014', '2013', '2012', '2011'];
            var networkId = "darwmh0d";
            var fullName = "Mohammed Al-Darwish";
            var result = {'yearList': yearList, 'networkId': networkId, 'fullName': fullName};
            return result;
        }


    angular.module('memoryServices', [])
        .factory('Employee', [
            function () {
                return {
                    query: function () {
                        return employees;
                    },
                    get: function (employee) {
                        return findById(parseInt(employee.employeeId));
                    }
                }

            }])
        .factory('Report', [
            function () {
                return {
                    query: function (queryName, org, year, quarter) {
                        return executeReport('ZAD_MAT_01', '99993122002', '2014', '1');
                    },
                    getVarList: function (queryName) {
                        return varList(queryName);
                    },

                    getVarInfo : function (varTechName) {
                        return varInfo(varTechName);
                    },
                    getF4Help : function (iobjnm, vname, queryName) {
                        return f4Help(iobjnm, vname, queryName);
                    }
                }

            }])

        .factory('AppSetting', [
            function () {
                return {
                    getSetting: function () {
                        return getAppSetting();
                    }
                }

            }])

        .factory('OrgTree', [
            function () {
                return {
                    query: function () {
                        return null;
                    }
                }
            }])

}());